<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/25
 * Time: 11:30
 */
namespace app\admin_menu\controller;

use app\admin\controller\AdminBase;
use app\common\model\AdminMenu;
use app\common\model\RoleUser;
use app\common\model\User;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Menu extends AdminBase
{
    public function __call($action = '', $params = array())
    {
        $status = Request::param('status', 1);
        $filter = [
            'parent_id' => 0,
            'status'    => $status,
        ];

        $admin_menu_list     = [];
        $admin_top_menu_list = AdminMenu::where($filter)->order("view_order")->select()->toArray();
        foreach ($admin_top_menu_list as $k=>$v) {
            $admin_menu_list[$k] = $v;
            // 获取次级菜单
            $children = AdminMenu::where(array('parent_id'=>$v['id']))->order("view_order")->select()->toArray();
            if ($children) {
                $admin_menu_list[$k]['children'] = $children;
            }
        }

        return View::fetch('admin_menu_list', [
            'status'          => $status,
            'admin_menu_list' => $admin_menu_list
        ]);
    }

    public function add()
    {
        $admin_top_menu_list = AdminMenu::where(['parent_id'=>0, 'status'=>1])->select()->toArray();
        return View::fetch('admin_menu_add',[
            'admin_top_menu_list' => $admin_top_menu_list
        ]);
    }

    /**
     * 菜单的保存、更新函数
     */
    public function save()
    {
        $id   = Request::post('id', 0);
        $data = Request::post('info', array());

        if (!isset($data['title']) || !$data['title']) {
            return _return('栏目标题不能为空');
        }

        if (!isset($data['module']) || !$data['module']) {
            return _return('栏目模块不能为空');
        }

        if ($id) {
            if ($data['admin_menu']['parent_id'] == $id) {
                return _return('自己不能是自己的父级');
            }
            AdminMenu::update($data, ['id'=>$id]);

        } else {
            AdminMenu::create($data);
        }

        return redirect(url('/admin_menu/menu/admin'));
        //return array('操作成功', 'success', 'admin_menu/controller');
    }

    /**
     * 菜单的编辑
     */
    public function edit()
    {
        $id = Request::get('id', 0);

        $admin_menu_info = _uri('admin_menu', ['id'=>$id]);
        if (!$admin_menu_info) {
            return _return('对不起，您操作的菜单不存在。');
        }

        $filter = array('parent_id'=>0);

        $admin_top_menu_list = AdminMenu::where($filter)->select()->toArray();

        return View::fetch('admin_menu_add',[
           'info'                => $admin_menu_info,
           'admin_top_menu_list' => $admin_top_menu_list
        ]);
    }

    /**
     * 菜单的删除，无status字段，直接删除
     */
    public function delete()
    {
        $id = Request::param('id');
        if (!$id) {
            return ['info'=>'请选择要删除的信息'];
        }

        $ids = explode(',', $id);
        foreach ($ids as $k=>$v) {
            $admin_menu_info = _uri('admin_menu', ['id'=>$v]);
            if (!$admin_menu_info) {
                continue;
            }

            // 判断是否存在子级菜单。如果有，则不让删除
            $children_fields = AdminMenu::where(array('parent_id'=>$v))->column('id');
            if (!empty($children_fields)) {
                return ['info'=>'对不起，请先删除子级菜单！'];
            }

           // _model('admin_menu')->delete($v);
            AdminMenu::where('id', $v)->delete();
        }

        return ['info'=>'ok'];
    }

}